package cn.have.service.impl;

import cn.have.dao.OrderDao;
import cn.have.domain.Order;
import cn.have.service.AccountService;
import cn.have.service.OrderService;
import cn.have.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName OrderServiceImpl
 * @Author 王新
 * @Date: 2022/2/3 8:01 星期四
 * @Version 1.0
 * @Description
 * 创建订单->调用库存服务扣减库存->调用账户服务扣减账户余额->修改订单状态
 * 简单说：下订单->减库存->减余额->改状态
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private AccountService accountService;
    @Resource
    private StorageService storageService;

    @Override
    public void create(Order order) {
        log.info("--->开始创建新订单");
        orderDao.create(order);

        log.info("--->订单开始调用库存，做扣减Count");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("--->订单开始调用库存，做扣减end");

        log.info("--->订单开始调用账户，做扣减Money");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("--->订单开始调用账户，做扣减end");

        log.info("------->order-service中修改订单状态");
        orderDao.update(order.getUserId(), 0);
        log.info("------->order-service中修改订单状态end");

        log.info("------->下单结束");
    }
}
